﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text;
using System.Collections;
using NS.BLL.Systems;
using NS.Model.Systems;
using NS.Utilities;

public partial class Systems_UserRoles : System.Web.UI.Page
{
    private long UserID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        UserID = long.Parse(Request.QueryString["UserID"]);

        if (!IsPostBack)
        {
            CheckBoxListDataBind();
            LoadUserRoles();
        }
    }

    private void CheckBoxListDataBind()
    {
        SysRole.DataSource = RoleService.GetRoleByWeights(RoleService.GetUserTopWeigh(CookieHelper.GetValue("uid")));
        SysRole.DataBind();
    }
    protected void LoadUserRoles()
    {
        string[] RolesArray = UsersInRolesService.GetRolesForUser(UserID.ToString());

        if (RolesArray.Length > 0)
        {
            foreach (string role in RolesArray)
            {
                ListItem LI = SysRole.Items.FindByValue(role);
                if (LI != null) LI.Selected = true;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            UsersInRolesService.DeleteUsersInRolesByUserID(UserID);
            foreach (ListItem UserRole in SysRole.Items)
            {
                if (UserRole.Selected)
                {
                    UsersInRolesService.AddUsersInRoles(UserID, UserRole.Value);
                    if (UserRole.Text == "洗车工")
                    {
                        UtilityService.UpdateFieldByPK("UserDetail", "IsTrader", new Object[] { 1 }, "UserID", UserID.ToString());
                    }
                }
            }
            MessageBox.AppendScript(this, "$(':input[id$=\"btnSearch\"]', window.parent.document.body).click();");
        }
        catch (Exception ex)
        {
            MessageBox.ShowError(this, "修改失败！错误信息：" + ex.Message);
        }
    }
}